package com.corechina.apex_osswords.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.corechina.apex_osswords.common.Response;
import com.corechina.apex_osswords.common.ShiroUtils;
import com.corechina.apex_osswords.common.WeiXinUserUtils;
import com.corechina.apex_osswords.domain.dao.CoreKnowledgeBaseMapper;
import com.corechina.apex_osswords.domain.entity.CoreKnowledgeBase;
import com.corechina.apex_osswords.domain.vo.CoreKnowledgeBaseVo;
import com.corechina.apex_osswords.service.ICoreFilesService;
import com.corechina.apex_osswords.service.ICoreKnowledgeBaseService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 知识库 服务实现类
 * </p>
 *
 * @author fyh
 * @since 2023-02-03
 */
@Service
public class CoreKnowledgeBaseServiceImpl extends ServiceImpl<CoreKnowledgeBaseMapper, CoreKnowledgeBase> implements ICoreKnowledgeBaseService {

    @Autowired
    private CoreKnowledgeBaseMapper coreKnowledgeBaseMapper;

    @Autowired
    private ICoreFilesService iCoreFilesService;

    @Override
    public Response add(CoreKnowledgeBase coreKnowledgeBase) {
        coreKnowledgeBase.setCreateTime(LocalDateTime.now());
        coreKnowledgeBase.setCreator(ShiroUtils.getUserId());
        coreKnowledgeBase.setEnterpriseId(ShiroUtils.getEnterpriseId());
        if (coreKnowledgeBaseMapper.insert(coreKnowledgeBase) > 0) {
            return Response.ok();
        }
        return Response.error();
    }


    @Override
    public Response getList(Integer type, String name ,Integer pageIndex, Integer pageSize) {
        IPage<CoreKnowledgeBaseVo> coreKnowledgeBaseVoIPage = coreKnowledgeBaseMapper.getList(new Page(pageIndex, pageSize), type, name, ShiroUtils.getSeeAll().equals(1) ? null : ShiroUtils.getEnterpriseId());
        return Response.ok(coreKnowledgeBaseVoIPage.getRecords(), coreKnowledgeBaseVoIPage.getTotal());
    }

    @Override
    public Response del(Integer id) {
        return null;
    }

    @Override
    public Response update(CoreKnowledgeBase coreKnowledgeBase) {
        return null;
    }

    @Override
    public Response getOne(Integer id) {
        CoreKnowledgeBaseVo coreKnowledgeBaseVo = coreKnowledgeBaseMapper.getOneForWeiXin(id);
        if (coreKnowledgeBaseVo.getCover() != null) {
            coreKnowledgeBaseVo.setCoverBase64(iCoreFilesService.getByGuidKey(coreKnowledgeBaseVo.getCover()).getImageBase64());
        }
        return Response.ok(coreKnowledgeBaseVo);
    }

    @Override
    public Response getListForWeiXin(Integer type,Integer pageIndex, Integer pageSize) {
        IPage<CoreKnowledgeBaseVo> coreKnowledgeBaseVoIPage = coreKnowledgeBaseMapper.getListForWeiXin(new Page(pageIndex, pageSize), type, WeiXinUserUtils.getEnterpriseId());
        for (CoreKnowledgeBaseVo coreKnowledgeBaseVo : coreKnowledgeBaseVoIPage.getRecords()) {
            if (coreKnowledgeBaseVo.getCover() != null) {
                coreKnowledgeBaseVo.setCoverBase64(iCoreFilesService.getByGuidKey(coreKnowledgeBaseVo.getCover()).getImageBase64());
            }
        }
        return Response.ok(coreKnowledgeBaseVoIPage.getRecords(), coreKnowledgeBaseVoIPage.getTotal());
    }

    @Override
    public Response getOneForWeiXin(Integer id) {
        CoreKnowledgeBaseVo coreKnowledgeBaseVo = coreKnowledgeBaseMapper.getOneForWeiXin(id);
        if (coreKnowledgeBaseVo.getCover() != null) {
                coreKnowledgeBaseVo.setCoverBase64(iCoreFilesService.getByGuidKey(coreKnowledgeBaseVo.getCover()).getImageBase64());
        }
        return Response.ok(coreKnowledgeBaseVo);
    }


}




